{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 利用矩阵转置判断矩阵是否对称\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "def is_symmetric_2(matrix):\n",
    "    rows = len(matrix)\n",
    "    cols = len(matrix[0])\n",
    "    # 首先判断矩阵是否为方阵\n",
    "    if rows != cols:\n",
    "        return False\n",
    "        # 获得转置矩阵\n",
    "    tranposed = [[(matrix[j][i])\n",
    "                  for j in range(rows)]\n",
    "                 for i in range(rows)]\n",
    "    if (matrix == tranposed):\n",
    "        return True\n",
    "    return False"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:19:04.890253Z",
     "end_time": "2024-07-03T16:19:04.894544Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 两个矩阵\n",
    "A = [[1, 2, 3],\n",
    "     [2, 4, 5],\n",
    "     [3, 5, 6]]\n",
    "\n",
    "B = [[1, 2, 3],\n",
    "     [2, 4, 0],\n",
    "     [0, 5, 6]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:19:12.584260Z",
     "end_time": "2024-07-03T16:19:12.594245Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否为对称矩阵: True\n"
     ]
    }
   ],
   "source": [
    "print(\"是否为对称矩阵:\", is_symmetric_2(A))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:19:23.588198Z",
     "end_time": "2024-07-03T16:19:23.596209Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否为对称矩阵: False\n"
     ]
    }
   ],
   "source": [
    "print(\"是否为对称矩阵:\", is_symmetric_2(B))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:19:34.117027Z",
     "end_time": "2024-07-03T16:19:34.125061Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
